package com.data.basic.chapter05;

/**
 * Created by wangbing on 2018/5/4.
 */
public class MergeTwoList {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        // 将链表2插进链表1
        ListNode cur = new ListNode(-1);
        ListNode root = cur;

        if (l1==null){
            return l2;
        }
        if (l2==null){
            return l1;
        }
        while(l1.next!=null&&l2.next!=null){
            if (l1.val<l2.val){
                 cur.next=l1;
                cur = cur.next;
                 l1 = l1.next;
            }
             else{
                 cur.next=l2;
                 cur = cur.next;
              l2 = l2.next;

         }

     }
        if(l1 != null){
            cur.next = l1;

        }
        if(l2 != null){
            cur.next = l2;

        }
        return root.next;

    }

}
